package com.ruoyi.report_lq.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_lq.domain.LqzrdjlbBsInfo;
import com.ruoyi.report_lq.domain.bo.LqzrdjlbBsInfoBo;
import com.ruoyi.report_lq.domain.vo.LqzrdjlbBsInfoVo;
import com.ruoyi.report_lq.mapper.LqzrdjlbBsInfoMapper;
import com.ruoyi.report_lq.service.ILqzrdjlbBsInfoService;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 沥青针入度记录主Service业务层处理
 *
 * @author zyb
 * @date 2024-06-11
 */
@RequiredArgsConstructor
@Service
public class LqzrdjlbBsInfoServiceImpl implements ILqzrdjlbBsInfoService {

    private final LqzrdjlbBsInfoMapper baseMapper;

    private final LqzrdjlbBaDetailServiceImpl detailService;

    private final LqydjlbBaDetailServiceImpl ydService;

    private final LqrhdjlbBaDetailServiceImpl rhdService;

    /**
     * 查询沥青针入度记录主
     */
    @Override
    @FIllBasicInfo
    public LqzrdjlbBsInfoVo queryById(Long excelId){
        LqzrdjlbBsInfoVo bsInfoVo = baseMapper.selectVoById(excelId);
        // TODO 获取针入度子表list数据
        bsInfoVo.setZrdList(detailService.queryByInfoId(bsInfoVo.getExcelId()));
        // TODO 获取沥青延度子表list数据
        bsInfoVo.setYdList(ydService.queryByInfoId(bsInfoVo.getExcelId()));
        // TODO 获取沥青软化点子表list数据
        bsInfoVo.setRhdList(rhdService.queryByInfoId(bsInfoVo.getExcelId()));
        return bsInfoVo;
    }

    /**
     * 查询沥青针入度记录主列表
     */
    @Override
    public TableDataInfo<LqzrdjlbBsInfoVo> queryPageList(LqzrdjlbBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<LqzrdjlbBsInfo> lqw = buildQueryWrapper(bo);
        Page<LqzrdjlbBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询沥青针入度记录主列表
     */
    @Override
    public List<LqzrdjlbBsInfoVo> queryList(LqzrdjlbBsInfoBo bo) {
        LambdaQueryWrapper<LqzrdjlbBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<LqzrdjlbBsInfo> buildQueryWrapper(LqzrdjlbBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<LqzrdjlbBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getQuydd()), LqzrdjlbBsInfo::getQuydd, bo.getQuydd());
        lqw.eq(StringUtils.isNotBlank(bo.getLiqcd()), LqzrdjlbBsInfo::getLiqcd, bo.getLiqcd());
        lqw.eq(StringUtils.isNotBlank(bo.getLiqzl()), LqzrdjlbBsInfo::getLiqzl, bo.getLiqzl());
        lqw.eq(StringUtils.isNotBlank(bo.getLiqbh()), LqzrdjlbBsInfo::getLiqbh, bo.getLiqbh());
        return lqw;
    }

    /**
     * 新增沥青针入度记录主
     */
    @Override
    public Boolean insertByBo(LqzrdjlbBsInfoBo bo) {
        boolean flag;
        LqzrdjlbBsInfo add = BeanUtil.toBean(bo, LqzrdjlbBsInfo.class);
        validEntityBeforeSave(add);
        flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }


    /**
     * 修改沥青针入度记录主
     */
    @Override
    public Boolean updateByBo(LqzrdjlbBsInfoBo bo) {
        boolean flag;
        LqzrdjlbBsInfo update = BeanUtil.toBean(bo, LqzrdjlbBsInfo.class);
        validEntityBeforeSave(update);
        baseMapper.updateById(update);
        //TODO 批量插入针入度记录子表
         detailService.insertOrUpdateByZrdBo(bo.getZrdList(),update.getExcelId());
        //TODO 批量插入沥青延度记录子表
        ydService.insertOrUpdateByZrdBo(bo.getYdList(),update.getExcelId());
        //TODO 批量插入沥青软化点记录子表
        flag = rhdService.insertOrUpdateByRhdBo(bo.getRhdList(),update.getExcelId());
        return flag;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(LqzrdjlbBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除沥青针入度记录主
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
